<?php

class ClixView
{
    protected $_scriptsPath = '';
    protected $_l = '';
    protected $_r = 0;
    public $_m = false;
    
    public function render($f)
    {
        $this->_r++;

        if (($this->_m)&&($this->exists("mobile/{$f}"))) {
            $f = "mobile/{$f}";
        }

        if (!$this->exists($f)) {
            throw new ClixViewException("View file not found '{$this->_scriptsPath}{$f}'.");
        }

        ob_start();
        include("{$this->_scriptsPath}{$f}");
        $c = ob_get_clean();

        if (($this->_r==1)and(!empty($this->_l))) {
            $this->_content = $c;
            $c = $this->render($this->_l);
        }

        return $c;
    }
    
    public function exists($f)
    {
        return file_exists("{$this->_scriptsPath}{$f}");
    }
    
    public function setScriptsPath($p)
    {
        if ($p[strlen($p-1)]!='/') {
            $p .= '/';
        }
        $this->_scriptsPath = $p;
    }
    
    public function setLayout($f)
    {
        if ($this->exists($f)) {
            $this->_l = $f;
        } else {
            throw new ClixViewException("Layout file not found '{$file}'. (script path: {$this->_scriptsPath})");
        }
    }
}
